package com.cloud.sysmana.entity.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.cloud.sysmana.entity.SysDepa;
import com.cloud.sysmana.entity.dao.ISysDepaDao;
import com.cloud.util.base.BaseHibernateDao;

/**
 * 部门管理Dao
 * @author gaof
 * @date 2016年12月20日
 */
public class SysDepaDao extends BaseHibernateDao<SysDepa, Integer> implements ISysDepaDao {
	
	//存放子部门id的List，list存放地址，不会随deparId不同而再次new一次，会出现和之前一样的值，所以不用该方法
	List<SysDepa> childList=new ArrayList<SysDepa>(); 
	
	 
	/**
	 * 获取所有部门信息
	 * @return
	 */
	@Override
	public List<SysDepa> depaList() {
		return this.getEntities("From SysDepa order by seq");
	}

	/**
	 * 添加部门
	 * @param depa
	 */
	@Override
	public void addDepa(SysDepa depa) {
		this.saveEntity(depa);
	}

	/**
	 * 通过部门id获取部门
	 * @param id
	 * @return
	 */
	@Override
	public SysDepa getDepaById(Integer id) {
		return this.findById(id);
	}

	/**
	 * 删除所有部门
	 */
	@Override
	public void delAllDepa() {
		this.executeQuery("delete from sys_depa");
	}

	/**
	 * 通过pid获取下级部门
	 * @param id
	 * @return
	 */
	@Override
	public List<SysDepa> listDepaByPId(Integer id) {
		return this.getEntities("From SysDepa where pid = " + id);
	}

	/**
	 * 删除部门列表
	 * @param depaList
	 */
	@Override
	public void delDepaList(List<SysDepa> depaList) {
		this.deleteAll(depaList);
	}

	/**
	 * 根据部门Code获取部门信息
	 * @param fjDcode
	 * @return
	 */
	@Override
	public SysDepa getDepaByCode(String fjDcode) {
       return this.getEntity("From SysDepa where depaCode =" + fjDcode );
	}

	/**
	 * 根据部门类型获取部门列表信息
	 * @param type
	 * @return
	 */
	@Override
	public List<SysDepa> getDepaListByType(String type) {
		return this.getEntities("From SysDepa where depaType = "+type+"");
	}

	/**
	 * 根据用户的部门ID 获取  将用户设想为叶子节点 ，通过部门PID，获取到根节点的 部门ID 集合
	 * @param deparId
	 * @return
	 */
	@Override
	public List<String> getDepaNameListById(Integer deparId) {
		// TODO Auto-generated method stub
		//String sql = "SELECT T2.* FROM (SELECT @r AS _id, ( SELECT @r=:pid FROM sys_depa WHERE depa_id = _id ) AS parent_id, @l=:@l+1 AS lvl FROM (SELECT @r=:20, @l=:0) vars, sys_depa h WHERE @r <> 0 ) T1 JOIN sys_depa T2 ON T1._id = T2.depa_id ORDER BY T1.lvl DESC";
		//System.out.println("==="+sql);
		//这里暂时使用for循环来获取吧 无语
		List<String> list = new ArrayList<String>();
		String sqltest = "select * from sys_depa where depa_id = "+deparId;
		int pid= ((SysDepa)this.getSQLQuery(sqltest, SysDepa.class).get(0)).getPid();
		list.add(((SysDepa)this.getSQLQuery(sqltest, SysDepa.class).get(0)).getDepaName());
		while(pid!=0){
			sqltest = "select * from sys_depa where depa_id = "+pid;
			pid= ((SysDepa)this.getSQLQuery(sqltest, SysDepa.class).get(0)).getPid();
			list.add(((SysDepa)this.getSQLQuery(sqltest, SysDepa.class).get(0)).getDepaName());
		}
		return list; 
	}
	/**
	 * 根据用户的部门ID 获取  将用户设想为叶子节点 ，通过部门PID，获取到根节点的 部门ID 拼接的字符串 用于in查询
	 * @param deparId
	 * @return
	 */
	@Override
	public String getDepaNameStrInById(Integer deparId) {
		// TODO Auto-generated method stub
		//这里暂时使用for循环来获取吧 无语
		String sqltest = "select * from sys_depa where depa_id = "+deparId;
		SysDepa depa = (SysDepa) this.getSQLQuery(sqltest, SysDepa.class).get(0);
		int pid= depa.getPid();
		String strIn = "'"+depa.getDepaName()+"'";
		while(pid!=0){
			sqltest = "select * from sys_depa where depa_id = "+pid;
			depa = (SysDepa) this.getSQLQuery(sqltest, SysDepa.class).get(0);
			pid= depa.getPid();
			strIn +=" , '"+ depa.getDepaName()+"'";
		}
		return strIn;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDepa> getDeparBySort() {
		// TODO Auto-generated method stub
		String slqString="select * from sys_depa sd order by sd.seq";
		return this.getSQLQuery(slqString, SysDepa.class);
	}


	/**
	 * list存放地址，不会随deparId不同而再次new一次，会出现和之前一样的值，所以不用该方法
	 */
	@Override
	public List<SysDepa> getDeparSonIdByPId(List<SysDepa> sysDeparlist,
			Integer deparId) {
		// TODO Auto-generated method stub
		for (SysDepa sysDepa : sysDeparlist) {
			// 遍历出pid等于deparId的子节点，add进子节点集合
			if (sysDepa.getPid() == deparId) {
				// 递归遍历下一级
				getDeparSonIdByPId(sysDeparlist, sysDepa.getDepaId());
				childList.add(sysDepa);
			}
		}
		return childList;
	}

}
